package com.ruoyi.system.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TPerson;
import com.ruoyi.system.service.ITPersonService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TTributeList;
import com.ruoyi.system.service.ITTributeListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 进贡管理Controller
 *
 * @author ruoyi
 * @date 2022-04-26
 */
@Controller
@RequestMapping("/tribute/list")
public class TTributeListController extends BaseController {
    private String prefix = "tribute/list";

    @Autowired
    private ITTributeListService tTributeListService;

    @Autowired
    private ITPersonService personService;

    @RequiresPermissions("tribute:list:view")
    @GetMapping()
    public String list() {
        return prefix + "/list";
    }

    /**
     * 查询进贡管理列表
     */
    @RequiresPermissions("tribute:list:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TTributeList tTributeList) {
        startPage();
        List<TTributeList> list = tTributeListService.selectTTributeListList(tTributeList);
        return getDataTable(list);
    }

    /**
     * 导出进贡管理列表
     */
    @RequiresPermissions("tribute:list:export")
    @Log(title = "进贡管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TTributeList tTributeList) {
        List<TTributeList> list = tTributeListService.selectTTributeListList(tTributeList);
        ExcelUtil<TTributeList> util = new ExcelUtil<TTributeList>(TTributeList.class);
        return util.exportExcel(list, "进贡管理数据");
    }

    /**
     * 新增进贡管理
     */
    @GetMapping("/add")
    public String add(@RequestParam(required = false) String personId, ModelMap mmap) {
        //查询信士列表
        TPerson queryItem = new TPerson();
        if (StringUtils.isNotEmpty(personId)&&(!personId.equals("null"))){
            queryItem.setId(personId);
        }
        List<TPerson> tPeople = personService.selectTPersonList(queryItem);
        mmap.put("personList", tPeople);
        return prefix + "/add";
    }


    /**
     * 新增保存进贡管理
     */
    @RequiresPermissions("tribute:list:add")
    @Log(title = "进贡管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TTributeList tTributeList) {
        return toAjax(tTributeListService.insertTTributeList(tTributeList));
    }

    /**
     * 修改进贡管理
     */
    @RequiresPermissions("tribute:list:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TTributeList tTributeList = tTributeListService.selectTTributeListById(id);
        mmap.put("tTributeList", tTributeList);
        return prefix + "/edit";
    }

    /**
     * 修改保存进贡管理
     */
    @RequiresPermissions("tribute:list:edit")
    @Log(title = "进贡管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TTributeList tTributeList) {
        return toAjax(tTributeListService.updateTTributeList(tTributeList));
    }

    /**
     * 删除进贡管理
     */
    @RequiresPermissions("tribute:list:remove")
    @Log(title = "进贡管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tTributeListService.deleteTTributeListByIds(ids));
    }

    /**
     * 导出人员列表
     */
    @Log(title = "人员", businessType = BusinessType.EXPORT)
    @GetMapping("/exportInfo")
    @ResponseBody
    public void export(@RequestParam(required = false) String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
        tTributeListService.exportInfo(id, response, request);
    }

}
